package com.block.merchant.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.constant.RedisKeyConstant;
import com.block.common.exception.NotifyExceptionService;
import com.block.db.entity.MerchantPushCountInfo;
import com.block.db.mapper.MerchantPushCountInfoMapper;
import com.block.merchant.model.dto.PushCountDTO;
import com.block.merchant.service.IMerchantPushCountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 商户推送数量统计 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-17
 */
@Service
public class MerchantPushCountInfoServiceImpl extends ServiceImpl<MerchantPushCountInfoMapper, MerchantPushCountInfo> implements IMerchantPushCountInfoService {

    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public void savePushResult(PushCountDTO pushCountDTO) {

        try {
            String dateInfo = LocalDate.now().toString();
            MerchantPushCountInfo  pushCountInfo = this.baseMapper.getEntityByDateAndTaskId(pushCountDTO.getTaskId(), dateInfo);
            if (ObjectUtil.isEmpty(pushCountInfo)){
                pushCountInfo = new MerchantPushCountInfo();
                pushCountInfo.setDateInfo(dateInfo);
                pushCountInfo.setInfoId(pushCountDTO.getInfoId());
                pushCountInfo.setInfoType(pushCountDTO.getInfoType());
                pushCountInfo.setTaskId(pushCountDTO.getTaskId());
                boolean save = this.save(pushCountInfo);
                if (!save){
                    return;
                }
                pushCountInfo = this.baseMapper.getEntityByDateAndTaskId(pushCountDTO.getTaskId(), dateInfo);
            }

            Integer successNumber = pushCountInfo.getSuccessNumber()+pushCountDTO.getSuccessNumber();

            MerchantPushCountInfo updateEntity = new MerchantPushCountInfo();
            updateEntity.setId(pushCountInfo.getId());
            updateEntity.setSuccessNumber(successNumber);
            updateEntity.setSettleNumber(pushCountInfo.getSettleNumber()+pushCountDTO.getSettleNumber());
            this.updateById(updateEntity);

            // 推送成功数据缓存
            String nowDate = LocalDate.now().toString();
            String redisKey = StrUtil.format(RedisKeyConstant.MERCHANT_TASK_PUSH_COUNT_KEY, nowDate,pushCountDTO.getTaskId());
            stringRedisTemplate.opsForValue().set(redisKey, successNumber+"", 1, TimeUnit.DAYS);

        }catch (Exception exception){
            exception.printStackTrace();
            notifyExceptionService.notifyNoticeInfo("COUNT_PUSH_INFO", "统计推送异常", JSON.toJSONString(pushCountDTO), exception);
        }

    }
}
